package com.grm.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.grm.async.CallRemoteMethodAsyncTask;
import com.grm.entity.Brand;
import com.grm.service.BrandService;

/**
 * desc: 讲解异步的业务实现层
 *
 * @author gaorimao
 * @since 2021-3-14
 */
@Service
public class BrandServiceImpl implements BrandService {
	
	private static Logger logger = LoggerFactory.getLogger(BrandServiceImpl.class);
	
	@Autowired
	private CallRemoteMethodAsyncTask asyncTask;

	/**
	 * desc: 异步实现查询远程接口
	 */
	@Override
	public List<Brand> queryBrands() {
		List<Brand> brands = new ArrayList<Brand>();
		//调用异步方法查询各种类型的商品
		Future<List<Brand>> phonesFuture = asyncTask.queryPhones();
		Future<List<Brand>> computersFuture = asyncTask.queryComputers();
		Future<List<Brand>> padsFuture = asyncTask.queryPads();
		
		List<Brand> phones = new ArrayList<Brand>();
		List<Brand> computers = new ArrayList<Brand>();
		List<Brand> pads = new ArrayList<Brand>();
		try {
			phones = phonesFuture.get();
			computers = computersFuture.get();
			pads = padsFuture.get();
			if(!CollectionUtils.isEmpty(phones)) {
				brands.addAll(phones);
			}
			if(!CollectionUtils.isEmpty(computers)) {
				brands.addAll(computers);
			}
			if(!CollectionUtils.isEmpty(pads)) {
				brands.addAll(pads);
			}
		} catch (InterruptedException e) {
			logger.error("[BrandServiceImpl] queryBrands interruptedException = {}",e);
		} catch (ExecutionException e) {
			logger.error("[BrandServiceImpl] queryBrands executionException = {}",e);
		}
		return brands;
	}

}
